package com.hxd.service.impl;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.hxd.base.BaseServiceImpl;
import com.hxd.common.utils.StringUtils;
import com.hxd.entity.SysConfig;
import com.hxd.mapper.SysConfigMapper;
import com.hxd.service.SysConfigService;


/**
 * <p>
    * 配置表 服务实现类
    * </p>
 *
 * @author zhoujw
 * @since 2019-12-18
 * @version v1.0
 */
@Service
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

	@Override
	public String getValue(String key) {
		SysConfig sysConfig = getOne(new QueryWrapper<SysConfig>().eq(SysConfig.Fields.ckey, key));
		return sysConfig != null ? sysConfig.getCvalue() : null;
	}
	
	public <T> T getConfigObject(String key, Class<T> clazz) {
        String value = getValue(key);
        if(StringUtils.isNotBlank(value)){
            return new Gson().fromJson(value, clazz);
        }
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("获取参数失败");
        }
    }

}
